package com.ww.sound_emission.utils;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.validation.Errors;
import org.springframework.validation.ObjectError;

import java.util.List;

@NoArgsConstructor
@AllArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Result<T> {
	private int code;
	private String msg;
	private T data;
	private int count;
	private List<String> name;
	private int pageCount;

	// 基础构造函数，仅包含 code 和 msg
	public Result(int code, String msg) {
		this.code = code;
		this.msg = msg;
	}

	// 包含 code, msg 和 data 的构造函数
	public Result(int code, String msg, T data) {
		this.code = code;
		this.msg = msg;
		this.data = data;
	}

	// 包含 code, msg, data 和 count 的构造函数
	public Result(int code, String msg, T data, int count) {
		this.code = code;
		this.msg = msg;
		this.data = data;
		this.count = count;
	}

	// 包含 code, msg, data, count 和 pageCount 的构造函数
	public Result(int code, String msg, T data, int count, int pageCount) {
		this.code = code;
		this.msg = msg;
		this.data = data;
		this.count = count;
		this.pageCount = pageCount;
	}

	// 包含 code, msg, name 和 data 的构造函数
	public Result(int code, String msg, List<String> name, T data) {
		this.code = code;
		this.msg = msg;
		this.data = data;
		this.name = name;
	}

	// 从 Errors 对象构造 Result
	public Result(Errors errors) {
		StringBuilder msg = new StringBuilder();
		errors.getFieldErrors().forEach((ObjectError error) -> {
			msg.append(error.getDefaultMessage()).append("\n");
		});

		this.code = 0;
		this.msg = msg.toString();
	}

	// 静态工厂方法：插入成功
	public static <T> Result<T> insertSuccess() {
		return new Result<>(200, "添加成功");
	}

	// 静态工厂方法：插入失败
	public static <T> Result<T> insertFailed() {
		return new Result<>(500, "添加失败");
	}

	// 静态工厂方法：删除成功
	public static <T> Result<T> deleteSuccess() {
		return new Result<>(200, "删除成功");
	}

	// 静态工厂方法：删除失败
	public static <T> Result<T> deleteFailed() {
		return new Result<>(500, "删除失败");
	}

	// 静态工厂方法：更新成功
	public static <T> Result<T> updateSuccess() {
		return new Result<>(200, "更新成功");
	}

	// 静态工厂方法：更新失败
	public static <T> Result<T> updateFailed() {
		return new Result<>(500, "更新失败");
	}

	// 静态工厂方法：超出库存数量
	public static <T> Result<T> overNumSuccess() {
		return new Result<>(500, "超出库存数量!");
	}

	// 静态工厂方法：成功返回数据
	public static <T> Result<T> success(T data) {
		return new Result<>(200, "操作成功", data);
	}

	// 静态工厂方法：成功返回数据及总条数
	public static <T> Result<T> success(T data, int count) {
		return new Result<>(200, "操作成功", data, count);
	}

	// 静态工厂方法：成功返回数据、总条数及总页数
	public static <T> Result<T> success(T data, int count, int pageCount) {
		return new Result<>(200, "操作成功", data, count, pageCount);
	}

	// 静态工厂方法：成功返回数据及名称列表
	public static <T> Result<T> success(T data, List<String> name) {
		return new Result<>(200, "操作成功", name, data);
	}

	// 静态工厂方法：成功返回消息
	public static <T> Result<T> success(String msg) {
		return new Result<>(200, msg);
	}

	// 静态工厂方法：失败返回消息
	public static <T> Result<T> fail(String msg) {
		return new Result<>(500, msg);
	}

	// Getter 和 Setter 方法
	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public List<String> getName() {
		return name;
	}

	public void setName(List<String> name) {
		this.name = name;
	}

	public int getPageCount() {
		return pageCount;
	}

	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}
}